/*
 *    Author: Zhenghuabin
 *   Created: Thu Jan 16 17:00:24 2014
 *
 */
#include "gtest/gtest.h"   
#include "asio_task_pool.h"
#include "test_callback.h"

TEST(COMMON, asio_task_pool) {
    const std::string host = "www.newsmth.net";
    const std::string port = "80";
    const std::string path = "/nForum/index";
    std::map<std::string, std::string> query_strings;
    std::string data;
    
    boost::shared_ptr<TestCallback> callback(new TestCallback());
    common::util::AsioTaskPool pool(8);
    pool.start();
    for (size_t i = 0; i < 100; ++i) {
        boost::shared_ptr<common::util::AsyncHttpClient> client(new common::util::AsyncHttpClient(pool.get_io_service()));
        pool.post(boost::bind(&common::util::AsyncHttpClient::perform, client, host, port, path, query_strings,
                              data, callback));
    }
    pool.wait();
    EXPECT_EQ(200, int(TestCallback::status_));  
}
TEST(COMMON, asio_task_pool_stop) {
  const std::string host = "www.newsmth.net";
    const std::string port = "80";
    const std::string path = "/nForum/index";
    std::map<std::string, std::string> query_strings;
    std::string data;

    boost::shared_ptr<TestCallback> callback(new TestCallback(false));
    common::util::AsioTaskPool pool(8);
    pool.start();
    for (size_t i = 0; i < 1000; ++i) {
        boost::shared_ptr<common::util::AsyncHttpClient> client(new common::util::AsyncHttpClient(pool.get_io_service()));
        pool.post(boost::bind(&common::util::AsyncHttpClient::perform, client, host, port, path, query_strings,
                              data, callback));
    }
    time_t t1 = time(NULL);
    LOG(INFO) << "before stop";
    pool.stop();
    pool.wait();
    time_t t2 = time(NULL);
    LOG(INFO) << "after wait";
    EXPECT_EQ(200, int(TestCallback::status_));
    EXPECT_LE(t2 - t1, 2);
}    
